#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#if defined(TEST_TARGET_check)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/check.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "check.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

typedef uint8_t (*checkType)(uint8_t* data, uint32_t len);

static checkType checkFunTable[] = {
    check_sum,
    check_parity,
    check_lrc,
    check_xor,
};

static void test_base(void)
{
    char *testSample[] = {
        "Hello",
        "check algorithms",
        "123456789",
    };
    uint8_t check = 0;
    for (int i = 0; i < sizeof(testSample) / sizeof(testSample[0]); i++)
    {
        printf("testSample %d\r\n", i);
        for (int j = 0; j < sizeof(checkFunTable) / sizeof(checkFunTable[0]); j++)
        {
            check = (checkFunTable[j])(testSample[i], strlen(testSample[i]));
            printf("Func %d, check %02X\r\n", j, check);
        }
    }
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: check [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <cal>       Calculate the check value\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -c <check>          Check algorithm, default `check_sum`\n"
"                        0: check_sum\n"
"                        1: check_parity\n"
"                        2: check_lrc\n"
"                        3: check_xor\n"
"    -s <string>         Check string\n"
"\n"
    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    checkType check = checkFunTable[0];
    char *s = NULL;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvc:s:");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'c' :
            {
                int index = 0;
                index = atoi(command_optarg);
                if (index >= (sizeof(checkFunTable) / sizeof(checkFunTable[0])))
                {
                    printf("No such check functiion!\r\n");
                    return -1;
                }
                check = checkFunTable[index];
            }
            break;
        case 's' :
            s = command_optarg;
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("check version %d.%d.%d\r\n", CHECK_V_MAJOR, CHECK_V_MINOR, CHECK_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "cal"))
        {
            if (!s)
            {
                printf("Use the -s option to specify the check string!\r\n");
                return -1;
            }
            printf("check value: 0x%X\r\n", check(s, strlen(s)));
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_check)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_check(void)
{
    command_export("check", test);

    // command("check");
    // command("check -e cal -c 0 -s 123456789");
    // command("check -e cal -c 1 -s 123456789");
    // command("check -e cal -c 2 -s 123456789");
    // command("check -e cal -c 3 -s 123456789");
}
init_export_app(test_check);
#endif 
